package com.it.jasypt.config;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LogbackConfig {
    @Bean
    public LoggerContext loggerContext() {
        LoggerContext context = new LoggerContext();

        // 添加脱敏转换规则
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(context);
        encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
        encoder.setOutputPatternAsHeader(false);

        // 自定义脱敏转换器
        context.putProperty("mask", MaskingConverter.class.getName());

        return context;
    }
    public static class MaskingConverter extends ClassicConverter {
        @Override
        public String convert(ILoggingEvent event) {
            String message = event.getFormattedMessage();
            // 实现脱敏逻辑，如身份证号、手机号等
            return message.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
    }
}
